/**
 * 
 */
package com.base.myproject.server;

import java.io.IOException;
import java.io.PrintWriter;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Enumeration;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.base.myproject.client.busi.service.Associator;
import com.base.myproject.client.system.Env;
import com.base.myproject.server.database.PoolMan;
import com.base.myproject.server.init.MyLogger;

/**
 * @author zgw
 *2009-11-5 下午06:52:42
 * javazgw@gmail.com
 */
public class FinaceServlet extends HttpServlet {
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	
	protected void processRequest(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		
		
		response.setContentType("text/html");
		response.setCharacterEncoding("UTF-8");
		PrintWriter out = response.getWriter();
		request.setCharacterEncoding("utf-8");
		
		Enumeration  enumeration=request.getParameterNames();
		
		while(enumeration.hasMoreElements())
		{
			String  eeee =""+ enumeration.nextElement();
			System.out.print("e="+eeee+"=");
			System.out.println(request.getParameter(""+eeee));
		}

	
		String action =request.getParameter("Action");
		if(action!=null && action.equals("addfince"))
		{
			String s_cardno =request.getParameter("s_cardno");
			String user =request.getParameter("user");
			String pass =request.getParameter("pass");
			String finace_real =request.getParameter("finace_real");
			String finace_gif =request.getParameter("finace_gif");
			String finace =request.getParameter("finace");
			
			System.out.println("ccode="+s_cardno);
			System.out.println("user="+user);
			System.out.println("pass="+pass);
			System.out.println("finace="+finace);
			
			String str;
//			boolean b = FindUser.isHaveUserbyName(user, pass);
			boolean b = FindUser.isHaveUser(user, pass);
			if(b)
			{
			PoolMan pm = new PoolMan();
			
			String selectsql = "select ccode,cname from ccode where s_cardno='"+s_cardno+"'";
			
			String ccode = null;
			String cname = "";
			ResultSet rs=null;
			try {
				 rs= pm.executeQuery(selectsql);
				if(rs.next())
				{
					ccode = rs.getString("ccode");
					cname = rs.getString("cname");
				}
			} catch (Exception e1) {
				// TODO Auto-generated catch block
				e1.printStackTrace();
			}
			finally
			{
				try {
					if(rs!=null)
					rs.close();
				} catch (SQLException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}
			
			if (ccode != null) {
					String sql = "insert into s_service_finance (ccode,finance,updatedate,dc,bcode,finance_real,finance_gif) values('"
							+ ccode
							+ "',"
							+ finace
							+ ",getdate(),1,'"
							+ user+"',"+finace_real+","+finace_gif
							+ ")";

					try {
						/**
						 * @Todo 此处应该进行同步，否则update成功，但是查询不成功则返回的信息是有问题的
						 * 
						 */
						pm.Update(sql);
						MyLogger.getInstance().debug("finance "+sql);
						//××××× 充值成功后查找余额
						GreetingServiceImpl g = new GreetingServiceImpl();
						Associator assoc = g.getAssociator(s_cardno, 4);
						
						//×××××
						
						
						str = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>";
						str += "<root status='" + Env.SUCCESS + "' cname ='"
								+ assoc.getName() + "' addfinance='"+finace+"' lastfinance='"+assoc.getFinance()+"' s_cardno='"+s_cardno+"'>";

						str += "</root>";
						out.print(str);
						return;
					} catch (Exception e) {
						str = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>";
						str +="<root status='"+Env.FALSE+"' message='未知错误，充值失败，请重新充值'>";
					
						str +="</root>";
						out.print(str);
						
						e.printStackTrace();
						return;
					}
				}
			else
			{
				str = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>";
				str +="<root status='"+Env.FALSE+"' message='没有找到会员信息'>";
			
				str +="</root>";
				out.print(str);
				return;
			}
			}
			str = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>";
			str +="<root status='"+Env.FALSE+"' message='操作员名或密码不正确'>";
		
			str +="</root>";
			out.print(str);
			return;
		}
	}
	protected void doGet(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		
		processRequest(request, response);
	}
	@Override
	protected void doPost(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		
		processRequest(request, response);
	}
}
